// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.

package org.columba.mail.gui.action;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import org.columba.api.gui.frame.IFrameMediator;
import org.columba.core.gui.action.AbstractColumbaAction;
import org.columba.core.gui.frame.DefaultContainer;
import org.columba.core.gui.frame.FrameManager;
import org.columba.core.resourceloader.GlobalResourceLoader;
import org.columba.mail.config.MailConfig;
import org.columba.mail.gui.composer.ComposerController;
import org.columba.mail.gui.config.accountwizard.AccountWizardLauncher;
import org.columba.mail.resourceloader.IconKeys;
import org.columba.mail.resourceloader.MailImageLoader;
import org.columba.mail.util.MailResourceLoader;

/**
 * Opens the composer window for creating a new message.
 */
public class NewMessageAction extends AbstractColumbaAction {
    private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";

	public NewMessageAction() {
		super(null, "New Message Action");
	}

	public NewMessageAction(IFrameMediator controller) {
		super(controller, MailResourceLoader.getString("menu", "mainframe",
				"menu_message_new"));
		putValue(TOOLBAR_NAME, MailResourceLoader.getString("menu",
				"mainframe", "menu_message_new_toolbar"));
		putValue(SHORT_DESCRIPTION, MailResourceLoader.getString("menu",
				"mainframe", "menu_message_new_tooltip").replaceAll("&", ""));
		putValue(SMALL_ICON, MailImageLoader.getSmallIcon(IconKeys.MESSAGE_NEW));
		putValue(LARGE_ICON, MailImageLoader.getIcon(IconKeys.MESSAGE_NEW));
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_M,
				Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
	}

	public void actionPerformed(ActionEvent evt) {

		// if no account exists, return
		if ( MailConfig.getInstance().getAccountList().count() == 0 ) {
			JOptionPane.showMessageDialog(FrameManager.getInstance()
					.getActiveFrame(), GlobalResourceLoader.getString(RESOURCE_PATH, "error",
			"no_account_defined"),"",JOptionPane.INFORMATION_MESSAGE);

			new AccountWizardLauncher().launchWizard(true);
			return;
		}

		ComposerController controller = new ComposerController();
		new DefaultContainer(controller);

		// model -> view
		controller.updateComponents(true);

	}
}